//
//  DASegmentedControl.m
//  aa
//
//  Created by DreamArts on 16/4/6.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DASegmentedControl.h"

@interface DASegmentedControl ()

@property (nonatomic, strong) NSArray *dataArray;           // 根据数组的个数分配每个label 显示的空间
@property (nonatomic, strong) UILabel *smallLabel;      // 每个用来显示的label
@property (nonatomic, strong) UIView *lineView;         // label 下面的线

@end

@implementation DASegmentedControl

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        // TODO  做一些默认的设置
    }
    return self;
}

#pragma mark - 类型枚举设置 
- (void)setStyle:(DASegmentedControlStyle)style {
    switch (style) {
        case DASegmentedControlStyleDefault:
            self.smallLabel.textAlignment = NSTextAlignmentCenter;
            self.smallLabel.textColor = [UIColor blueColor];
            self.smallLabel.font = [UIFont systemFontOfSize:14];
//            self.layer.borderColor = [UIColor colorWithRed:0.00 green:0.48 blue:1.00 alpha:1.00].CGColor;
//            self.layer.borderWidth = 1;
            break;
            
        default:
            break;
    }
    
}

#pragma mark - 垂直的情况下
- (void)creatVerticalView {
    CGFloat height = self.frame.size.height / self.dataArray.count;   // 平均每个小view 的高度
    for (NSInteger i = 0; i < self.dataArray.count; i++) {
        self.smallLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, i * height, self.frame.size.width, height - 0.5)];
        if (i > 0) {
            self.lineView = [[UIView alloc]initWithFrame:CGRectMake(0, i * height - 0.5, self.frame.size.width, 0.5)];
        }
        [self addSubview:self.lineView];
        [self addSubview:self.smallLabel];
        self.smallLabel.tag = 1000 + i;
        self.smallLabel.userInteractionEnabled = YES;
        self.smallLabel.text = self.dataArray[i];
        [self setStyle:self.segmentedControlStyle];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewTap:)];
        [self.smallLabel addGestureRecognizer:tap];
        
        if(i == 0) {
            
            self.smallLabel.backgroundColor = [UIColor colorWithRed:0.00 green:0.48 blue:1.00 alpha:1.00];
            self.smallLabel.textColor =  [UIColor whiteColor];
        }else{
            
            self.smallLabel.backgroundColor = [UIColor whiteColor];
            self.lineView.backgroundColor = [UIColor colorWithRed:0.00 green:0.48 blue:1.00 alpha:1.00];
        }
    }
}
// 垂直的时候 触发的手势方法
- (void)viewTap:(UITapGestureRecognizer *)tap {
    // 用来返回点击了第几个label
    self.block(tap.view.tag - 1000);
    for (NSInteger i = 0; i < self.dataArray.count; i++) {
        UILabel *label = (UILabel *)[self viewWithTag:1000 + i];
        label.backgroundColor = [UIColor whiteColor];
        label.textColor = [UIColor colorWithRed:0.00 green:0.48 blue:1.00 alpha:1.00];
    }
    UILabel *label = (UILabel *)[self viewWithTag:tap.view.tag];
    label.backgroundColor = [UIColor colorWithRed:0.00 green:0.48 blue:1.00 alpha:1.00];
    label.textColor = [UIColor whiteColor];
}
#pragma mark - 水平的情况下
- (void)creatHorizontalView {
    
    CGFloat width = self.frame.size.width / self.dataArray.count;    // 平均每个小view 的宽度
    for(NSInteger i = 0; i < self.dataArray.count; i++) {
        self.smallLabel = [[UILabel alloc]initWithFrame:CGRectMake(i * width, 0, width - 0.5, self.frame.size.height)];
        if (i > 0) {
            self.lineView = [[UIView alloc]initWithFrame:CGRectMake(i * width - 0.5, 0, 0.5, self.frame.size.height)];
        }
        [self addSubview:self.smallLabel];
        [self addSubview:self.lineView];
        self.smallLabel.tag = 1000 + i;
        self.smallLabel.text = self.dataArray[i];
        [self setStyle:self.segmentedControlStyle];
        self.smallLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(smallViewTap:)];
        [self.smallLabel addGestureRecognizer:tap];
        if (i == 0) {
            
            self.smallLabel.backgroundColor = [UIColor colorWithRed:0.00 green:0.48 blue:1.00 alpha:1.00];
            self.smallLabel.textColor =  [UIColor whiteColor];
        } else{
            
            self.smallLabel.backgroundColor = [UIColor whiteColor];
            self.lineView.backgroundColor = [UIColor colorWithRed:0.00 green:0.48 blue:1.00 alpha:1.00];
        }
    }
}
// 水平的情况下 触发的手势方法
- (void)smallViewTap:(UITapGestureRecognizer *)tap {
    
    // 用来返回点击了第几个label
    self.block(tap.view.tag - 1000);
    for (NSInteger i = 0; i < self.dataArray.count; i++) {
        UILabel *label = (UILabel *)[self viewWithTag:1000 + i];
        label.backgroundColor = [UIColor whiteColor];
        label.textColor = [UIColor colorWithRed:0.00 green:0.48 blue:1.00 alpha:1.00];
    }
    UILabel *label = (UILabel *)[self viewWithTag:tap.view.tag];
    label.backgroundColor = [UIColor colorWithRed:0.00 green:0.48 blue:1.00 alpha:1.00];
    label.textColor = [UIColor whiteColor];
}
#pragma mark - 添加segementedControl 的设置方法
- (void)addSegmentedControlInView:(UIView *)superView style:(DASegmentedControlStyle)segmentedStyle type:(DASegmentedControlType)segmentedType array:(NSArray *)Array onTouchBlock:(TouchUpInsideBlocks)Block {
    if (Array) {
        self.dataArray = Array;
    }
    if (superView) {
        [superView addSubview:self];
    }
    if (segmentedType) {
        self.segmentedControlType = segmentedType;
        
    }
    switch (segmentedType) {
        case DASegmentedControlTypeVertical:
            [self creatVerticalView];
            break;
        case DASegmentedControlTypeHorizontal:
            [self creatHorizontalView];
        default:
            [self creatVerticalView];     // 默认垂直
            break;
    }
    self.block = Block;
    
}

#pragma mark - 拖拽
- (BOOL)dragEnabled {
    return NO;   // 默认不允许拖拽
}

- (void)setDragEnabled:(BOOL)dragEnabled {
    if (dragEnabled) {
        self.userInteractionEnabled = YES;
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panchAction:)];
        [self addGestureRecognizer:pan];
    } else{
        self.userInteractionEnabled = NO;
    }
}

// 拖拽手势触发的方法
- (void)panchAction:(UIPanGestureRecognizer *)pan {
    
    DASegmentedControl *button = (DASegmentedControl *)pan.view;
    CGPoint p = [pan translationInView:button];
    [button setTransform:CGAffineTransformTranslate(button.transform, p.x, p.y)];
    [pan setTranslation:CGPointZero inView:button];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
